package com.itheima.user.controller;

import com.itheima.pojo.User;
import com.itheima.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")

    public User getUser(@PathVariable Long id) {
        User user = userService.getUser(id);
        return user;
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order.queue"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.DIRECT),
            key = {"red"}
    ))
    public void Listen(Map map){
        Object userId = map.get("userId");
        Object orderNum = map.get("orderNum");
        User user = userService.getUser(Long.valueOf(String.valueOf(userId)));
        log.info("恭喜您，您的订单:{}=>>{}{}已经创建成功!!", userId, user, orderNum);
    }
}
